package com.uphone.edwards.pro.activity.login.mine;

import android.arch.lifecycle.LifecycleOwner;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.radish.baselibrary.utils.EncryptUtils;
import com.radish.baselibrary.utils.LogUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.edwards.Contancts;
import com.uphone.edwards.MyApp;
import com.uphone.edwards.base.mvp.BasePAV;
import com.uphone.edwards.http.ApiService;
import com.uphone.edwards.http.RxSchedulers;
import com.uphone.edwards.pro.activity.login.LoginContract;
import com.uphone.edwards.pro.activity.login.register.RegisterContract;
import com.uphone.edwards.util.SharePreUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MinePresenter extends BasePAV<MineContract.View> implements MineContract.Presenter {

    @Inject
    MinePresenter() {

    }

    @Override
    public void postMine() {
        mView.showLoading();
//        Map<String, String> map = new HashMap<>();
//        map.put("langId", "2");
        MyApp.apiService(ApiService.class)
                .getUserInfo(MyApp.languageType , SharePreUtils.init(MyApp.getmContext()).getString(Contancts.TOKEN))
                .compose(RxSchedulers.io_main())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(bean -> {
                    mView.closeLoading();
                    if (bean != null) {
                        try {
                            String add = bean.string();
                            LogUtils.e("====" + add);

                            JSONObject jsonObject = new JSONObject(add);
                            int status = jsonObject.getInt("code");
                            LogUtils.e("status:" + status + "  responseBean:" + bean.string());
                            if (status != 1) {
                                ToastUtils.showShort(jsonObject.getString("msg"));
                            }
                            mView.downMineView(add , status);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, throwable -> {
                    mView.closeLoading();
                    mView.onFail();
                });
    }

    @Override
    public void postDownLoadMine(String employeesNo , String title , String userName
            , String enterpriseEmail , String department , String location) {
        mView.showLoading();
        MyApp.apiService(ApiService.class)
                .modifyUserInfo(MyApp.languageType , SharePreUtils.init(MyApp.getmContext()).getString(Contancts.TOKEN) , employeesNo , title
                        , userName , enterpriseEmail , department , location)
                .compose(RxSchedulers.io_main())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(bean -> {
                    mView.closeLoading();
                    if (bean != null) {
                        try {
                            String add = bean.string();

                            JSONObject jsonObject = new JSONObject(add);
                            int status = jsonObject.getInt("code");
                            LogUtils.e("status:" + status + "  responseBean:" + bean.string());
                            if (status != 1) {
                                ToastUtils.showShort(jsonObject.getString("msg"));
                            }
                            mView.mineView(add , status);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, throwable -> {
                    mView.closeLoading();
                    mView.onFail();
                });
    }



}